﻿<%@ WebHandler Language="C#" Class="ZSEntrustMeasurementReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 装配试车厂委托计量报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.11.24
 **/

public class ZSEntrustMeasurementReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @"
SELECT em.productname,p.description,em.entrustqty,e.fullname,em.createdate,to_char(em.reservedate,'yyyy-mm-dd') reservedate,
       DECODE(em.entruschecktype,1,'特种检验',2,'无损检验',3,'理化检验','') entruschecktype,
       e2.fullname treateperson,em.engineno,em.batchno,em.treatdate,e2.employeename,emd.checkpointnote,emd.checkpointresultnote
FROM EntrustMeasurement em
LEFT JOIN product p ON em.productid = p.productid
LEFT JOIN entrustmeasurementdetail emd ON emd.entrustmeasurementid = em.entrustmeasurementid
LEFT JOIN employee e ON e.employeeid = em.entrustorid --提交人
LEFT JOIN employee e2 ON e2.employeeid = em.TreatEmployeeID --检定人
LEFT JOIN factory f ON em.factoryid = f.factoryid
WHERE 1=1 ";
        if (jObject["factoryname"] != null && jObject["factoryname"].ToString() != "")
        {
            sql += string.Format(" AND f.factoryname = '{0}'", jObject["factoryname"].ToString());
        }
        
        //物料号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND em.productname LIKE '%{0}%'", jObject["productname"].ToString());
        }

        //送检日期
        if (jObject["createdate"] != null && jObject["createdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createdate"].ToString());
            if (arr.Count >= 2)
            {
                sql += string.Format(" AND TRUNC(em.createdate) >= to_date('{0}','yyyy-MM-dd')", arr[0].ToString());
                sql += string.Format(" AND TRUNC(em.createdate) <= to_date('{0}','yyyy-MM-dd')", arr[1].ToString());
            }
        }

        //提交人
        if (jObject["creater"] != null && jObject["creater"].ToString() != "")
        {
            sql += string.Format(" AND e.fullname LIKE '%{0}%'", jObject["creater"].ToString());
        }

        //检定人
        if (jObject["jdr"] != null && jObject["jdr"].ToString() != "")
        {
            sql += string.Format(" AND e2.fullname LIKE '%{0}%'", jObject["jdr"].ToString());
        }

        //送检类型
        if (jObject["entruschecktype"] != null && jObject["entruschecktype"].ToString() != "")
        {
            sql += string.Format(" AND em.entruschecktype = '{0}'", jObject["entruschecktype"].ToString());
        }

        //发动机号
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            sql += string.Format(" AND em.engineno = '{0}'", jObject["engineno"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dt.Columns["productname"].Caption = "物料号";
        dt.Columns["description"].Caption = "物料名称";
        dt.Columns["entrustqty"].Caption = "数量";
        dt.Columns["fullname"].Caption = "创建人";
        dt.Columns["createdate"].Caption = "创建时间";
        dt.Columns["reservedate"].Caption = "预定取件时间";
        dt.Columns["entruschecktype"].Caption = "检验类型";
        dt.Columns["treateperson"].Caption = "检定人";
        dt.Columns["engineno"].Caption = "发动机号";
        dt.Columns["batchno"].Caption = "批次号";
        dt.Columns["treatdate"].Caption = "检验时间";
        dt.Columns["employeename"].Caption = "员工号";
        dt.Columns["checkpointnote"].Caption = "内容要求值";
        dt.Columns["checkpointresultnote"].Caption = "内容实际值";
        ExportHelper.ExportExcel(dt, jObject["factoryname"].ToString() + "委托计量查询报表");
    }
}